package com.softWang.common.response;

import com.softWang.common.exception.ErrorCode;
import lombok.Data;

import java.io.Serializable;

/**
 * 自定义响应返回值
 */
@Data
public class ResponseResult<T> implements Serializable {
    /**
     * @Fields serialVersionUID : TODO(用一句话描述这个变量表示什么)
     */
    private static final long serialVersionUID = -4461307964012001505L;

    private int code;
    private String message;
    private T data;


    private ResponseResult() {

    }

    /**
     * 返回成功后的响应码和响应描述
     *
     * @return
     */
    public static ResponseResult success() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.code= ErrorCode.SUCCESS.code;
        responseResult.message=ErrorCode.SUCCESS.desc;
        return responseResult;
    }

    /**
     * 返回成功后的响应码和响应描述以及数据
     *
     * @param data 数据
     * @return
     */
    public static ResponseResult success(Object data) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.code=ErrorCode.SUCCESS.code;
        responseResult.message=ErrorCode.SUCCESS.desc;
        responseResult.data=data;
        return responseResult;
    }


    /**
     * 返回http响应
     *
     * @param errorCode 错误码
     * @return
     */
    public static ResponseResult failure(ErrorCode errorCode) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.code=errorCode.code;
        responseResult.message=errorCode.desc;
        return responseResult;
    }

    /**
     * 返回http响应
     *
     * @param errorCode 错误码
     * @param data      响应数据
     * @return
     */
    public static ResponseResult failure(ErrorCode errorCode, Object data) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.code=errorCode.code;
        responseResult.message=errorCode.desc;
        responseResult.data=data;
        return responseResult;
    }

    /**
     * 返回默认系统异常响应
     *
     * @return
     */
    public static ResponseResult sysError() {
        ResponseResult responseResult = new ResponseResult();
        responseResult.code=ErrorCode.EXCEPTION_SYS.code;
        responseResult.message=ErrorCode.EXCEPTION_SYS.desc;
        return responseResult;
    }

    /**
     * 返回系统异常响应
     *
     * @param exception 系统异常描述
     * @return
     */
    public static ResponseResult sysError(String exception) {
        ResponseResult responseResult = new ResponseResult();
        responseResult.code=ErrorCode.EXCEPTION_SYS.code;
        responseResult.message=ErrorCode.EXCEPTION_SYS.desc + exception;
        return responseResult;
    }



    /**
     * 追加响应错误描述信息
     *
     * @param message 响应描述信息
     * @return
     */
    public ResponseResult appendMessage(String message) {
        this.message=(this.message == null ? "" : this.message.toString()) + message;
        return this;
    }

}
